//
//  SSBaseTabBarController.m
//  starspaceVR
//
//  Created by 曹端芹 on 17/9/12.
//  Copyright © 2017年 starspace. All rights reserved.
//

#import "SSBaseTabBarController.h"

#import "SSUserInfoManager.h"
#import "SSBaseNavigationController.h"
#import "SSHandpickViewController.h"
#import "SSClassifyViewController.h"
#import "SSSearchViewController.h"
#import "SSMineViewController.h"
#import "SSLoginViewController.h"

@interface SSBaseTabBarController () <UITabBarControllerDelegate>

@end

@implementation SSBaseTabBarController

+ (void)initialize {

    [[UITabBar appearance] setTranslucent:NO];
    [UITabBar appearance].barTintColor = [UIColor whiteColor];
    
    UITabBarItem *item = [UITabBarItem appearance];
    //item.titlePositionAdjustment = UIOffsetMake(0, 1.5);
    
    NSMutableDictionary *selectAtts = [NSMutableDictionary dictionary];
    selectAtts[NSFontAttributeName] = kFont(13);
    selectAtts[NSForegroundColorAttributeName] = kRGBColor(223, 45, 36);
    
    [item setTitleTextAttributes:selectAtts forState:UIControlStateSelected];
    

    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess) name:kLoginSuccessKey object:nil];
}

- (void)loginSuccess {
    self.selectedIndex = 3;

}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    [self addChildViewControllerWithClassName:[SSHandpickViewController description] imageName:@"jingxuan" title:@"精选"];
    [self addChildViewControllerWithClassName:[SSClassifyViewController description] imageName:@"fenlei" title:@"分类"];
    [self addChildViewControllerWithClassName:[SSSearchViewController description] imageName:@"sousuo" title:@"搜索"];
    [self addChildViewControllerWithClassName:[SSMineViewController description] imageName:@"yonghu" title:@"个人中心"];
    
    
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
    if (index == 3) {
        if ([[SSUserInfoManager sharedManager] isLogin]) {
            return YES;
        }else {
          SSLoginViewController *loginVC = [[UIStoryboard storyboardWithName:@"SSLogin" bundle:nil] instantiateInitialViewController];
            [tabBarController presentViewController:loginVC animated:YES completion:nil];
            return NO;
        }
    }else{
        return YES;
    }
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) addChildViewControllerWithClassName:(NSString *)classname
                                   imageName:(NSString *)imageName
                                       title:(NSString *)title {

    UIViewController *vc = [[NSClassFromString(classname) alloc] init];
    SSBaseNavigationController *nav = [[SSBaseNavigationController alloc] initWithRootViewController:vc];
    nav.tabBarItem.title = title;
    nav.tabBarItem.image = [UIImage imageNamed:imageName];
    nav.tabBarItem.selectedImage = [[UIImage imageNamed:[imageName stringByAppendingString:@"_selected"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self addChildViewController:nav];

}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (BOOL)shouldAutorotate {
    return NO;
}


@end
